using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Data.Linq;
using System.Xml.Linq;

namespace Memopad.Biz
{
    public partial class MemoManager
    {
        public SortedDictionary<string, string> GetSubjectBodyDictionary(int minReadCount)
        {
            var list = Get(x => x.ReadCount >= minReadCount);

            var dictionary = new SortedDictionary<string, string>();

            foreach (var memo in list)
                dictionary.Add(memo.Subject, memo.Body);

            return dictionary;
        }

        public List<Memo> GetChildren(int memoID)
        {
            MemopadDataContext dataContext = ManagerRepository.CreateDataContext(false, false);

            var result = dataContext.Memo_GetChildren(memoID);

            return ToCleanCollection(result);
        }

        public List<Memo> GetList()
        {
            MemopadDataContext dataContext = ManagerRepository.CreateDataContext(false, false);

            var query = from x in dataContext.Memos
                        select x;

//            return ToCleanCollection(query);
                        return ToCleanCollection<List<Memo>>(query);
        }

        public BindingList<Memo> GetBindingList()
        {
            MemopadDataContext dataContext = ManagerRepository.CreateDataContext();
            var query = from x in dataContext.Memos
                        select x;

            return ToCleanCollection<BindingList<Memo>>(query);
        }

        public bool InsertInTransaction(Memo memo0, Memo memo1)
        {
            MemopadDataContext dataContext = ManagerRepository.CreateDataContext();

            dataContext.Memos.InsertOnSubmit(memo0);
            dataContext.Memos.InsertOnSubmit(memo1);

            try
            {
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool UpdateInTransaction(Memo memo0, Memo memo1)
        {
            MemopadDataContext dataContext = ManagerRepository.CreateDataContext();

            Memo clone0 = memo0.Clone();
            dataContext.Memos.Attach(clone0, true);

            Memo clone1 = memo1.Clone();
            dataContext.Memos.Attach(clone1, true);

            try
            {
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteInTransaction(Memo memo0, Memo memo1)
        {
            MemopadDataContext dataContext = ManagerRepository.CreateDataContext();

            Memo clone0 = memo0.Clone();
            dataContext.Memos.Attach(clone0, true);
            dataContext.Memos.DeleteOnSubmit(clone0);

            Memo clone1 = memo1.Clone();
            dataContext.Memos.Attach(clone1, true);
            dataContext.Memos.DeleteOnSubmit(clone1);

            try
            {
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}